﻿using RTW.OA.Attributes;
using Newtonsoft.Json;
using RTW.OA.Areas.Personal.Models;
using RTW.OA.Areas.Personal.Servers;
using RTW.OA.Areas.System.Servers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Personal.Controllers
{
    public class DiaryController : Controller
    {
        private DiaryServers diaryServers;
        private DeptServers deptServers;
        private UserServers userServers;

        public DiaryController()
        {
            diaryServers = new DiaryServers();
            deptServers = new DeptServers();
            userServers = new UserServers();
        }

        [CheckLogin(IsChildPage = true)]
        // GET: Personal/Diary
        public ActionResult Search()
        {
            var depts = deptServers.GetData(null,null,null);
            foreach (var item in depts)
            {
                item.users = userServers.GetDataByDRPT_ID(item.DEPT_ID);
            }
            var userinfo = Session["UserInfo"] as user;
            var model = diaryServers.GetData(userinfo.USER_ID);
            foreach (var item in model)
            {
                item.diary_comments = diaryServers.GetDiaryComment(item.DIA_ID);
            }
            ViewBag.depts = depts;
            return View(model);
        }

        public ActionResult GetDiarySearch(int type, int id, string querydate)
        {
            try
            {
                var model = diaryServers.GetDiarySearchData(type,id,querydate);
                foreach (var item in model)
                {
                    item.diary_comments = diaryServers.GetDiaryComment(item.DIA_ID);
                }
                return Json(new
                {
                    code = 100,
                    data = model,
                    msg = "查询成功"
                }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return Json(new
                {
                    code = 0,
                    msg = "查询失败，原因：" + e.Message
                }, JsonRequestBehavior.AllowGet);
            }
        }

        [CheckLogin(IsChildPage = true)]
        // GET: Personal/Diary
        public ActionResult Index()
        {
            const int MY_DIARY_FLAG = 1;
            const int OTHER_DIARY_FLAG = 2;
            var userinfo = Session["UserInfo"] as user;

            DiaryCollection diaryCollection = new DiaryCollection();
            var allDiary = diaryServers.GetData(userinfo.USER_ID);
            foreach (var item in allDiary)
            {
                item.diary_comments = diaryServers.GetDiaryComment(item.DIA_ID);
            }
            diaryCollection.all = allDiary;
            var myDiary = diaryServers.GetData(userinfo.USER_ID, MY_DIARY_FLAG);
            foreach (var item in myDiary)
            {
                item.diary_comments = diaryServers.GetDiaryComment(item.DIA_ID);
            }
            diaryCollection.my = myDiary;
            var otherDiary = diaryServers.GetData(userinfo.USER_ID, OTHER_DIARY_FLAG);
            foreach (var item in otherDiary)
            {
                item.diary_comments = diaryServers.GetDiaryComment(item.DIA_ID);
            }
            diaryCollection.other = otherDiary;

            return View(diaryCollection);
        }

        [CheckLogin(IsChildPage = true)]
        // GET: Personal/Diary
        public ActionResult Details(int id)
        {
            diary model = new diary();
            diaryServers.UpdateReaders(id);
            model = diaryServers.GetModelById(id);
            model.USER = diaryServers.GetUsers(model.USER_ID);
            model.diary_comments = diaryServers.GetDiaryComment(id);
            foreach (var item in model.diary_comments)
            {
                item.user = diaryServers.GetUsers(item.USER_ID);
                item.diary_comment_reply = diaryServers.GetDiaryCommentReplay(item.COMMENT_ID);
            }

            var userinfo = Session["UserInfo"] as user;
            ViewBag.TRUE_NAME = userinfo.TRUE_NAME;
            return View(model);
        }

        public ActionResult AddComment(int id, string content)
        {
            try
            {
                diary_comment model = new diary_comment();
                var userinfo = Session["UserInfo"] as user;
                model.DIA_ID = id;
                model.CONTENT = content;
                model.USER_ID = userinfo.USER_ID;
                model.SEND_TIME = DateTime.Now;
                diaryServers.AddComment(model);

                return Json(new
                {
                    code = 100,
                    data = model,
                    msg = "添加成功"
                }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return Json(new
                {
                    code = 0,
                    msg = "添加失败，原因：" + e.Message
                }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult AddCommentReply(int id, string reply_to, string content)
        {
            try
            {
                diary_comment_reply model = new diary_comment_reply
                {
                    COMMENT_ID = id,
                    REPLY_COMMENT = content,
                    REPLY_TIME = DateTime.Now
                };
                var replyToUser = diaryServers.GetUsers(reply_to);
                model.TO_ID = replyToUser.USER_ID;
                var users = Session["UserInfo"] as user;
                model.REPLY_ID = users.USER_ID;
                model.REPLYER = users.TRUE_NAME;
                diaryServers.AddCommentReply(model);

                return Json(new
                {
                    code = 100,
                    data = model,
                    msg = "添加成功"
                }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return Json(new
                {
                    code = 0,
                    msg = "添加失败，原因：" + e.Message
                }, JsonRequestBehavior.AllowGet);
            }
        }

        // GET: Personal/Diary/Edit/5
        public ActionResult Edit(int? id)
        {
            ViewBag.DIA_TYPEData = diaryServers.GetDIA_TYPE();
            var model = new diary();
            if (id != null)
            {
                ViewBag.Title = "编辑工作日志表";
                model = GetDiaryModel(id.Value);
            }
            else
            {
                ViewBag.Title = "新建工作日志表";
            }
            return View(model);
        }

        // POST: Personal/Diary/Edit/5
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int? id, diary model)
        {
            ViewBag.DIA_TYPEData = diaryServers.GetDIA_TYPE();
            try
            {
                var userinfo = Session["UserInfo"] as user;
                model.USER_ID = userinfo.USER_ID;
                if (id == null || id == 0)
                {
                    model.DIA_TIME = DateTime.Now;
                    diaryServers.Add(model);
                    ViewBag.msg = "新增工作日志表信息成功！";
                    model = new diary();
                }
                else
                {
                    model.DIA_TIME = DateTime.Now;
                    model.COMPRESS_CONTENT = Encoding.UTF8.GetBytes(model.CONTENT);
                    diaryServers.Update(model);
                    ViewBag.msg = "修改工作日志表信息成功！";

                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: Personal/Diary/Delete/5
        [HttpPost]
        public JsonResult DiaryDelete(int id)
        {
            try
            {
                if (id != 0)
                {
                    //diaryServers.Del(id.ToString());
                    ViewBag.msg = "删除工作日志表信息成功！";
                }
                return Json(new
                {
                    code = 100,
                    msg = "删除成功"
                }, JsonRequestBehavior.AllowGet);
            }
            catch(Exception e)
            {
                return Json(new
                {
                    code = 0,
                    msg = "删除失败，原因：" + e.Message
                }, JsonRequestBehavior.AllowGet);
            }
        }

        // GET: Personal/Diary/Delete/5
        [HttpPost]
        public JsonResult DiaryShare(int id, short isShared)
        {
            try
            {
                diaryServers.SetSharedStatus(id, isShared);
                return Json(new
                {
                    code = 100,
                    msg = "设置分享状态成功"
                }, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new
                {
                    code = 0,
                    msg = "设置分享状态失败"
                }, JsonRequestBehavior.AllowGet);
            }
        }

        #region //GetData
        public string GetTableJsonResult()
        {
            var list = diaryServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

        public diary GetDiaryModel(int id)
        {
            var model = diaryServers.GetModelById(id);
            return model;
        }

        #endregion //GetData
    }
}